
function validateEditLeave(){

	var numOfRows = document.getElementById("numOfRows").value;
	var i=0;
	var errorMessage = "";
	var errorFlag = 0;
	var result = "";
	
	for (i=0;i<numOfRows;i++) {
		
		var lN = "leaveNum"+i;
		var dF = "dateOfFiling"+i;
		var dR = "dateReceived"+i;
		var tL = "typeOfLeave"+i;
		var d = "days"+i;
		var sD = "startDate"+i;
		var eD = "endDate"+i;
		var s = "status"+i;
		
		var leaveNum = document.getElementById(lN).innerHTML;
		var dateOfFiling = document.getElementById(dF).value;
		var dateReceived = document.getElementById(dR).value;
		var typeOfLeave = document.getElementById(tL).value;
		var days = document.getElementById(d).value;
		var startDate = document.getElementById(sD).value;
		var endDate = document.getElementById(eD).value;
		var status = document.getElementById(s).value;
		var dateOfFilingFlag = false, dateReceivedFlag = false;
		var startDateFlag = false, endDateFlag = false, daysFlag=false;			
		var	daysComputed = 0;
		var daysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
		
			
		//validate type of leave
		if(typeOfLeave == "null")
		{
			errorMessage += leaveNum+": No type of leave selected.\n";
			errorFlag = 1;
			document.getElementById(tL).style.backgroundColor="#FFFF99";
		} else {
			document.getElementById(tL).style.backgroundColor="white";
		}
		
		
		//validate days
		if(days.length == 0 ){
			
			errorMessage += leaveNum+": Days empty.\n";
			errorFlag = 1;
			daysFlag = false;
			document.getElementById(d).style.backgroundColor="#FFFF99";
			
		} else {
			daysFlag = true;
			document.getElementById(d).style.backgroundColor="white";
		}
		
		//validate start date
		if(checkFormat(startDate)==false) {
		   
			errorMessage += leaveNum+": Incorrect start date.\n";
			errorFlag = 1;
			startDateFlag = false;
			document.getElementById(sD).style.backgroundColor="#FFFF99";
			
		} else {
			
			//call function to check date
			result = checkDate(startDate.substring(0,2),
					  startDate.substring(3,5),
					  startDate.substring(6,10));
									  
			if ( result==false ) {
				errorMessage+= leaveNum+": Incorrect start date\n";
				errorFlag = 1;
				startDateFlag = false;
				document.getElementById(sD).style.backgroundColor="#FFFF99";
			} else {
				startDateFlag = true;
				document.getElementById(sD).style.backgroundColor="white";
			}
		}
		
		//validate end date
		if(checkFormat(endDate)==false) {
		   
			errorMessage += leaveNum+": Incorrect end date.\n";
			errorFlag = 1;
			endDateFlag = false;
			document.getElementById(eD).style.backgroundColor="#FFFF99";
		} else {
			
			//call function to check date
			result = checkDate(endDate.substring(0,2),
					  endDate.substring(3,5),
					  endDate.substring(6,10));
					  
			if ( result==false ) {
				document.getElementById(eD).style.backgroundColor="#FFFF99";
				errorMessage += leaveNum+": Incorrect end date.\n";
				errorFlag = 1;
				
				endDateFlag = false;
				
			} else {
				endDateFlag = true;
				document.getElementById(eD).style.backgroundColor="white";
			}
		}
		
		if(status == "null"){
			errorMessage += leaveNum+": No type of status selected.\n";
			errorFlag = 1;
			document.getElementById(s).style.backgroundColor="#FFFF99";
		}
		
		if (startDateFlag==true && endDateFlag==true && daysFlag==true) {
									
				//if the year in end date is earlier than start date
				if (endDate.substring(6,10) <
							startDate.substring(6,10)) {
							errorMessage += leaveNum+": Incorrect end date.\n";
							errorFlag = 1;
							document.getElementById(eD).style.backgroundColor="#FFFF99";
				} else {
					if (endDate.substring(0,2) < 
						startDate.substring(0,2)) {
						errorMessage += leaveNum+": Incorrect end date.\n";
						errorFlag = 1;
						document.getElementById(eD).style.backgroundColor="#FFFF99";
					} else if (endDate.substring(0,2) ==
							   startDate.substring(0,2)){
						if (endDate.substring(3,5) < startDate.substring(3,5)) {
							errorMessage += leaveNum+": Incorrect end date.\n";
							errorFlag = 1;
							document.getElementById(eD).style.backgroundColor="#FFFF99";
						} else {
							document.getElementById(eD).style.backgroundColor="white";
						}
					} 
				} 
			
																
				//checks if the months are equal
				if (startDate.substring(0,2)==endDate.substring(0,2)) {
									
					daysComputed = (parseInt(endDate.substring(3,5), 10)-
									parseInt(startDate.substring(3,5), 10))+1;
									
				}
				//if there is one month difference
				else if (startDate.substring(0,2)-endDate.substring(0,2)==1 || 
						 startDate.substring(0,2)-endDate.substring(0,2)==-1) {
					
					if(daysInMonth[startDate.substring(0,2)-1] == 31) {
						daysComputed = parseInt(31 - startDate.substring(3,5), 10)+
									   parseInt(endDate.substring(3,5), 10)+1;
					} else if (daysInMonth[startDate.substring(0,2)-1] == 30){
						daysComputed = parseInt(30 - startDate.substring(3,5), 10)+
									   parseInt(endDate.substring(3,5), 10)+1;
					} else if (daysInMonth[startDate.substring(0,2)-1] == 28){
						daysComputed = parseInt(28 - startDate.substring(3,5), 10)+
									   parseInt(endDate.substring(3,5), 10)+1;
					}
				
				}
				//if the month has more than one difference
				else {
					errorMessage += leaveNum+": Days exceed maximum number.\n";
					errorFlag = 1;
					document.getElementById(d).style.backgroundColor="#FFFF99";
				}
				
			
			//Checks if the start date is december, and end date is january
				if (parseInt(endDate.substring(6,10), 10)-
					parseInt(startDate.substring(6,10), 10) == 1) {
					
					if (startDate.substring(0,2) == 12 && 
						endDate.substring(0,2) == 1) {
						daysComputed = (31-parseInt(startDate.substring(3,5), 10))+
										parseInt(endDate.substring(3,5), 10) + 1;
						
					} else {
						errorMessage += leaveNum+": Incorrect number of days.\n";
						errorFlag = 1;
						document.getElementById(d).style.backgroundColor="#FFFF99";
					}
				}
		}
			
			//if the number of days computed is not equal to the number of days specified
			if (daysComputed != days) {
				errorMessage += leaveNum+": Incorrect number of days.\n";
				errorFlag = 1;
				document.getElementById(d).style.backgroundColor="#FFFF99";
			}
			
	}	

	//summary of errors
	if( errorFlag > 0) {
		alert(errorMessage);				
		return false;
	} else {
		return true;
	}						
	
}

function checkFormat (date) {

	if (date.length == 0 || date.length != 10 || 
	   date.charAt(2)!='/' || date.charAt(5)!='/' ||
	   isNaN(date.substring(0,2)) || 
	   isNaN(date.substring(3,5)) ||
	   isNaN(date.substring(6,10))) {
		return false;
	   }

	return true;

}

function checkDate ( month, day, year ) {

	var errorFlag = 0;
	year = parseInt(year, 10);
	
	if (day>=1 && month>=1 &&  month<=12 && year>=2000) {
						
		//months with 31 days
		if (month==1 || month==3 || month == 5 || month==7 || 
			 month==8 || month==10 || month == 12) {
			
			if ( day>31 ) {
				errorFlag = 1;
			}
			
		} else {
			
			if ( month == 2 ) {
				if ( year%4 == 0) { 
					if ( day>29 ) {
						errorFlag = 1;
					}
				} else {
					if ( day>28) {
						errorFlag = 1;
					}
				}							
			} else {
				if ( day>30 ) {
					errorFlag = 1;
				}							
			}						
		}
	
	} else {					
		errorFlag = 1;
	}
	
	if ( errorFlag==1 ) {
		return false;
	} else {
		return true;
	}

}
